import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_levin/config/app_manager.dart';
import 'package:flutter_levin/ui/pages/main/home_page.dart';
import 'package:flutter_levin/ui/pages/main/knowledge_page.dart';
import 'package:flutter_levin/ui/pages/main/mine_page.dart';
import 'package:flutter_levin/ui/pages/main/store_page.dart';
import 'package:logger/logger.dart';
import 'package:oktoast/oktoast.dart';

class MainTabPage extends StatefulWidget {
  MainTabPage({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MainTabState();
  }
}

class _MainTabState extends State<MainTabPage> {
  var _pageController = PageController();
  int _currentIndex = 0;
  int _lastExitAt = 0;

  @override
  Widget build(BuildContext context) {

    var logger = Logger();

    Brightness brightness = Theme.of(context).brightness;
    bool isDark = Brightness.dark == brightness;

    logger.d('------------$isDark');

    return Scaffold(
      body: WillPopScope(
        onWillPop: _exit,
        child: PageView.builder(
          itemBuilder: (ctx, index) => pages[index],
          itemCount: pages.length,
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: Theme.of(context).primaryColor,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
                color: Theme.of(context).accentColor,
              ),
              title: Text(
                '资讯',
                style: TextStyle(color: Theme.of(context).accentColor),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.lightbulb_outline,
                color: Theme.of(context).accentColor,
              ),
              title: Text(
                '知识',
                style: TextStyle(color: Theme.of(context).accentColor),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.list,
                color: Theme.of(context).accentColor,
              ),
              title: Text(
                '书架',
                style: TextStyle(color: Theme.of(context).accentColor),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.person,
                color: Theme.of(context).accentColor,
              ),
              title: Text(
                '我的',
                style: TextStyle(color: Theme.of(context).accentColor),
              )),
        ],
        currentIndex: _currentIndex,
        onTap: (index) {
          _pageController.jumpToPage(index);
        },
      ),
    );
  }

  Future<bool> _exit() {
    int currentAt = new DateTime.now().millisecondsSinceEpoch;
    if (currentAt - _lastExitAt > 1500) {
      showToast('退出APP');
      _lastExitAt = currentAt;
    } else {
      AppManager.pop();
    }
    return new Future.value(false);
  }

  List<Widget> pages = <Widget>[
    HomePage(),
    KnowledgePage(),
    StorePage(),
    MinePage()
  ];
}
